package com.sailorj.service.impl;

import com.sailorj.entity.TbWork;
import com.sailorj.mapper.TbWorkMapper;
import com.sailorj.service.TbWorkService;
import com.sailorj.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TbWorkServiceImpl implements TbWorkService {

    private static final Logger logger = LoggerFactory.getLogger(TbWorkServiceImpl.class);

    @Autowired
    public TbWorkMapper tbWorkMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public void addWork(TbWork tbWork) {
        tbWorkMapper.insert(tbWork);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public void updateWorkById(TbWork tbWork) {
        tbWorkMapper.updateByPrimaryKey(tbWork);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public TbWork getWorkById(int id) {
        return tbWorkMapper.selectByPrimaryKey(id);
    }

//    @Override
//    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
//    public List<TbWork> getWorkList() {
//        return tbWorkMapper.selectWorkList();
//    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public PageBean getWorkList(Integer page, Integer size) {
        if(page!=null&&size!=null){
            page=(page-1)*size;
        }
        List<TbWork> data = tbWorkMapper.selectWorkList(page,size);
        Long total=tbWorkMapper.getTotal();
        PageBean bean = new PageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public PageBean getWorkListByTime(Integer page, Integer size, String time) {
        if(page!=null&&size!=null){
            page=(page-1)*size;
        }
        List<TbWork> data = tbWorkMapper.getWorkListByTime(page,size,time);
        for (TbWork t:data) {
            logger.info(t.toString());
        }
        Long total=tbWorkMapper.getTotal();
        PageBean bean = new PageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }
}
